package cn.wolfcode.ssm.web.controller;

import cn.wolfcode.ssm.domain.SystemDictionary;
import cn.wolfcode.ssm.domain.SystemDictionaryItem;
import cn.wolfcode.ssm.qo.QueryObject;
import cn.wolfcode.ssm.qo.SystemDictionaryItemQueryObject;
import cn.wolfcode.ssm.service.ISystemDictionaryItemService;
import cn.wolfcode.ssm.service.ISystemDictionaryService;
import cn.wolfcode.ssm.util.RequireAnnotation;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/systemDictionaryItem")
public class SystemDictionaryItemController {

    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;
    @Autowired
    private ISystemDictionaryService systemDictionaryService;

    @RequestMapping("list")
    @RequireAnnotation(name = "字典明细页面",expression = "systemDictionaryItem:list")
    public String listAll(Model model, @ModelAttribute("qo") SystemDictionaryItemQueryObject qo){
        PageInfo<SystemDictionaryItem> pageInfo = systemDictionaryItemService.query(qo);
        List<SystemDictionary> dictionaries = systemDictionaryService.ListAll();
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("dictionaries",dictionaries);
        return "systemDictionaryItem/list";
    }

    @RequireAnnotation(name = "字典明细删除",expression = "systemDictionaryItem:delete")
    @RequestMapping("delete")
    public String delete(Long id){
        systemDictionaryItemService.deleteById(id);
        return "redirect:/systemDictionaryItem/list";
    }

    @RequireAnnotation(name = "字典明细编辑/保存",expression = "systemDictionaryItem:saveOrUpdate")
    @RequestMapping("input")
    public String input(Model model,Long id){

        if(id != null){
            SystemDictionaryItem systemDictionaryItem = systemDictionaryItemService.selectById(id);

            model.addAttribute("systemDictionaryItem",systemDictionaryItem);
        }
        return "systemDictionaryItem/input";
    }

    @RequireAnnotation(name = "字典明细编辑/保存",expression = "systemDictionaryItem:saveOrUpdate")
    @RequestMapping("saveOrUpdate")
    public String saveOrUpdate(Model model,SystemDictionaryItem systemDictionaryItem){

        if(systemDictionaryItem.getId() != null){
            systemDictionaryItemService.updateById(systemDictionaryItem);
        }else {
            systemDictionaryItemService.save(systemDictionaryItem);
        }
        return "redirect:/systemDictionaryItem/list";
    }

    @RequestMapping("selectItem")
    @ResponseBody
    public List<SystemDictionaryItem> selectItem(Long id){
        if(id!=null){
            return systemDictionaryItemService.selectByTypeId(id);
        }
    return null;

    }

    @RequestMapping("selectItemByParentId")
    @ResponseBody
    public List<SystemDictionaryItem> selectItemByParentId(Long parentId){
        if(parentId!=null){
            return systemDictionaryItemService.selectItemByParentId(parentId);
        }
        return null;

    }



}
